import request from "@/utils/request";
import type { ApiResponse, PageResult } from "@/types";

// 帖子相关类型
export interface PostCreateRequest {
  title: string;
  content: string;
  summary?: string;
  imageUrl?: string;
  videoUrl?: string;
  categoryId: number;
  tags?: string[];
  status?: number;
  isTop?: boolean;
  allowComment?: boolean;
  priority?: number;
}

export interface PostUpdateRequest extends PostCreateRequest {
  id: number;
}

export interface PostQueryRequest {
  page?: number;
  size?: number;
  keyword?: string;
  categoryId?: number;
  authorId?: number;
  status?: number;
  isTop?: boolean;
  sortBy?: string;
  sortOrder?: string;
  startDate?: string;
  endDate?: string;
}

export interface PostDTO {
  id: number;
  title: string;
  content: string;
  summary?: string;
  imageUrl?: string;
  videoUrl?: string;
  tags?: string[];
  status: number;
  isTop: boolean;
  allowComment?: boolean;
  viewCount: number;
  commentCount: number;
  likeCount: number;
  priority?: number;
  createdAt: string;
  updatedAt: string;
  author: {
    id: number;
    username: string;
    nickname?: string;
    avatar?: string;
  };
  category: {
    id: number;
    name: string;
    description?: string;
    icon?: string;
    color?: string;
  };
  isLiked?: boolean;
  isFavorited?: boolean;
}

export interface CategoryDTO {
  id: number;
  name: string;
  description?: string;
  icon?: string;
  color?: string;
  sort: number;
  isActive: boolean;
  createTime: string;
  updateTime: string;
  postCount: number;
}

/**
 * 创建帖子
 */
export function createPost(data: PostCreateRequest): Promise<PostDTO> {
  return request({
    url: "/posts",
    method: "post",
    data,
  });
}

/**
 * 更新帖子
 */
export function updatePost(postId: number, data: PostUpdateRequest): Promise<PostDTO> {
  return request({
    url: `/posts/${postId}`,
    method: "put",
    data,
  });
}

/**
 * 删除帖子
 */
export function deletePost(postId: number): Promise<ApiResponse> {
  return request({
    url: `/posts/${postId}`,
    method: "delete",
  });
}

/**
 * 获取帖子详情
 */
export function getPostById(postId: number): Promise<PostDTO> {
  return request({
    url: `/posts/${postId}`,
    method: "get",
  });
}

/**
 * 查询帖子列表
 */
export function getPosts(params: PostQueryRequest): Promise<PageResult<PostDTO>> {
  return request({
    url: "/posts",
    method: "get",
    params,
  });
}

/**
 * 获取热门帖子
 */
export function getHotPosts(page = 1, size = 10): Promise<PageResult<PostDTO>> {
  return request({
    url: "/posts/hot",
    method: "get",
    params: { page, size },
  });
}

/**
 * 点赞帖子
 */
export function likePost(postId: number): Promise<ApiResponse> {
  return request({
    url: `/posts/${postId}/like`,
    method: "post",
  });
}

/**
 * 取消点赞
 */
export function unlikePost(postId: number): Promise<ApiResponse> {
  return request({
    url: `/posts/${postId}/like`,
    method: "delete",
  });
}

/**
 * 获取置顶帖子
 */
export function getTopPosts(page = 1, size = 5): Promise<PageResult<PostDTO>> {
  return request({
    url: "/posts/top",
    method: "get",
    params: { page, size },
  });
}

/**
 * 根据分类获取帖子
 */
export function getPostsByCategory(
  categoryId: number,
  page = 1,
  size = 10,
  sortBy = "createTime",
  sortOrder = "DESC"
): Promise<PageResult<PostDTO>> {
  return request({
    url: `/posts/category/${categoryId}`,
    method: "get",
    params: { page, size, sortBy, sortOrder },
  });
}

/**
 * 根据作者获取帖子
 */
export function getPostsByAuthor(
  authorId: number,
  page = 1,
  size = 10,
  sortBy = "createTime",
  sortOrder = "DESC"
): Promise<PageResult<PostDTO>> {
  return request({
    url: `/posts/author/${authorId}`,
    method: "get",
    params: { page, size, sortBy, sortOrder },
  });
}

/**
 * 获取分类列表
 */
export function getCategories(): Promise<CategoryDTO[]> {
  return request({
    url: "/categories",
    method: "get",
  });
}
